package com.henghai.zhenxuan;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 1. InstrumentationProxy继承Instrumentation
 * 2. InstrumentationProxy持有Instrumentation实例的引用
 * 3. 实现execStartActivity方法，并在内部通过反射调用Instrumentation的execStartActivity方法
 */
public class InstrumentationProxy extends Instrumentation {
    private static final String TAG = "InstrumentationProxy";

    private Instrumentation mInstrumentation;

    public InstrumentationProxy(Instrumentation instrumentation) {
        mInstrumentation = instrumentation;
    }

    public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target,
                                            Intent intent, int requestCode, Bundle options) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ComponentName component = intent.getComponent();
        if (component != null) {
//            Log.i(TAG, "execStartActivity() >>> componentName:" + component.getClassName() +
//                    " packageName:" + component.getPackageName());
        }
//        Log.i(TAG, "execStartActivity() >>> " + "who:" + who + " scheme:" + intent.getScheme()+"  target:"+target.getClass().getName());

        //屏蔽beizi
        if (target != null && target.getClass().getName().startsWith("com.beizi.ad")) {
            Method execstartActivityMethod = Instrumentation.class.getDeclaredMethod("execStartActivity",
                    Context.class, IBinder.class, IBinder.class, Activity.class,
                    Intent.class, int.class, Bundle.class);
            return (ActivityResult) execstartActivityMethod.invoke(mInstrumentation, who, contextThread, token, target, intent, requestCode, options);
        }

        try {
            //屏蔽快应用
            //在这里判断scheme，处理自己的逻辑
            String scheme = intent.getScheme();
            //如果判定了是快应用的跳转，则直接return，屏蔽不跳转。
            if (!TextUtils.isEmpty(scheme) && (scheme.startsWith("hap") || scheme.startsWith("hwfastapp"))) {
                return null;
            }
            Method execStartActivityMethod = Instrumentation.class.getDeclaredMethod("execStartActivity",
                    Context.class, IBinder.class, IBinder.class, Activity.class,
                    Intent.class, int.class, Bundle.class);
            return (ActivityResult) execStartActivityMethod.invoke(mInstrumentation, who, contextThread, token, target,
                    intent, requestCode, options);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }
}
